setting.callback.onClick = getOrgan;
$(function(){
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_机构管理_列表,
			callback:action,
			con:"#datagrid-toolbar"}
	findResourcesByUserId(authCondition);
});

function getOrgan(event,treeId,treeNode){
	$.ajax({
		type : "GET",
		url : ctx + "/organ/query",
		data :{id:treeNode.id},
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border : false,
				fitColumns : true,
				checkbox : true,
				fit : true,
				remoteSort : false,
				rownumbers : true,
				singleSelect : true,
				toolbar : "#datagrid-toolbar",
				data : data.content
			});
		}
	});
}

/**
 * 添加
 */
function add(){
	var organTree = $.fn.zTree.getZTreeObj("organTree");
	var nodes = organTree.getSelectedNodes();
	if(nodes.length == 0){
		message("请选中父节点");
		return;
	}
	
	$('#dialog').dialog({
	    title: '添加',
	    width: 350,
	    height: 200,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/organ/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var name = $("#name").textbox("getValue");
            	if($.trim(name).length == 0){
            		return;
            	}
            	
            	var data = {};
            	data.name = name;
            	if(nodes.length != 0){
	            	data.pid = nodes[0].id;
            	}
            	
            	$.ajax({
            		type : "POST",
            		url : ctx + "/organ/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			
            			$('#datagrid').datagrid('appendRow',data.content);
            			if(!nodes[0].isParent){
            				nodes[0].isParent = true;
            			}
            			organTree.reAsyncChildNodes(nodes[0], "refresh");
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 修改
 */
function edit(){
	var row = undefined;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '修改',
	    width: 350,
	    height: 200,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/organ/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#name").textbox("setValue",row.name);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var name = $("#name").textbox("getValue");
            	if($.trim(name).length == 0){
            		return;
            	}
            	
            	var data = {};
            	data.id = row.id;
            	data.name = name;
            	
            	$.ajax({
            		type : "POST",
            		url : ctx + "/organ/update",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
            			var index = $('#datagrid').datagrid('getRowIndex',row);
            			
            			$('#datagrid').datagrid('updateRow',{
            				index: index,
        					row: {name:data.content.name}
        				});
            			
            			var organTree = $.fn.zTree.getZTreeObj("organTree");
            			var node = organTree.getNodeByParam("id", row.id, null);
            			node.name = data.content.name;
            			organTree.updateNode(node);
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 删除
 */
function del(){
	var row = undefined;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要删除吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/organ/delete",
	    		data :{id : row.id},
	    		dataType : "json",
	    		success : function(data) {
        			if(!showMsg(data,"删除成功")){
        				return;
        			}
	    			
        			var index = $('#datagrid').datagrid('getRowIndex',row);
	    			
	    			$("#datagrid").datagrid('deleteRow',index);
	    			var organTree = $.fn.zTree.getZTreeObj("organTree");
	    			var node = organTree.getNodeByParam("id", row.id, null);
	    			organTree.removeNode(node);
	    		}
	    	});
	    }
	});
}

/**
 * 统一设置编码为空的机构
 */
function setCode(){
	
	$.messager.confirm('提示', '确定要设置编码吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/organ/code",
	    		dataType : "json",
	    		success : function(data) {
        			if(!showMsg(data,"编码成功")){
        				return;
        			}
	    			var treeObj = $.fn.zTree.getZTreeObj("organTree");
	    			treeObj.refresh();
	    		}
	    	});
	    }
	});
}